<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class GoodsComment extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'goods_comment';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [

    ];







    public function getCreateTimeAttr($value, $data)
    {
        $value = $value ? $value : ($data['createtime'] ? $data['createtime'] : '');
        return $value ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getImagesAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['images']) ? $data['images'] : '');
        if(!$value){
            return [];
        }
        $value=explode(',',$value);
        foreach ($value as $k=>&$v){
            $v=get_app_image_url($v);
        }
        return $value;
    }

    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function goods()
    {
        return $this->belongsTo('Goods', 'goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    /*
     * 评价
     * $goods_id 商品id ，$user_id 用户id，$content内容，$level星级，$images图片，$sku_name规格名
     * */
    public static function getAdd($goods_id,$user_id,$content,$level,$images,$sku_name){
        $data['goods_id'] = $goods_id;
        $data['user_id'] = $user_id;
        $data['content'] = $content;
        $data['level'] = $level;
        $data['images'] = $images;
        $data['sku_name'] = $sku_name;
        return self::create($data);
    }
}
